package com.topera.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topera.eduservice.entity.EduSubject;
import com.topera.eduservice.entity.excel.SubjectData;
import com.topera.eduservice.entity.subject.SubjectNode;
import com.topera.eduservice.listener.SubjectListener;
import com.topera.eduservice.mapper.EduSubjectMapper;
import com.topera.eduservice.service.EduSubjectService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author puzhenjiang
 * @since 2020-05-17
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //ServiceImpl 中默认已经帮我们 注入了baseMapper
    //或者通过this.方法  用当前service进行查询

    @Override
    public void addSubject(MultipartFile file,EduSubjectService eduSubjectService) {

        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e){

        }
    }

    /**
     * 获取课程分类信息--树表展示
     * @return
     */
    @Override
    public List<SubjectNode> getAllSubject() {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        List<EduSubject> subjectList = baseMapper.selectList(wrapper);

        List<SubjectNode> subjectNodeList = getChildNodes(subjectList);

        return subjectNodeList;
    }

    /**
     * 递归获取子节点
     * @param subjectList
     * @return
     */
    private List<SubjectNode> getChildNodes(List<EduSubject> subjectList){
        List<SubjectNode> subjectNodeList = new ArrayList<>();
        subjectList.stream().forEach((subject)->{
            SubjectNode  subjectNode = new SubjectNode();
            subjectNode.setId(subject.getId());//id
            subjectNode.setLabel(subject.getTitle());//显示文字

            //从参数1中复制与参数2同名的参数 并赋值给参数2
            //BeanUtils.copyProperties(subject,subjectNode);//

            //查询子节点
            QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
            wrapper.eq("parent_id",subject.getId());
            List<EduSubject> childList = baseMapper.selectList(wrapper);
            List<SubjectNode> childNodeList = new ArrayList<>();
            childList.stream().forEach((child)->{
                SubjectNode  childNode = new SubjectNode();
                childNode.setId(child.getId());//id
                childNode.setLabel(child.getTitle());//显示文字
                //BeanUtils.copyProperties(child,childNode);//效果同上
                childNodeList.add(childNode);
            });
            subjectNode.setChildren(childNodeList);
            if(childList!=null && childList.size()>0){
                getChildNodes(childList);
            }
            subjectNodeList.add(subjectNode);
        });
        return subjectNodeList;
    }
}
